#include "charswidget.h"

CharsWidget::CharsWidget()
{
this->setAttribute( Qt::WA_DeleteOnClose );
setupUi(this);

readSettings();

default_font = QApplication::font();
clipboard = QApplication::clipboard();

bool ok = false;

    if ( true != unicode_font_str.isEmpty() )
    {
    ok = unicode_font.fromString(unicode_font_str);
    }

    if ( true != ok )
    {
    QFontDatabase * fdb = new QFontDatabase;
    QStringList ff = fdb->families();
        if ( ff.contains( QLatin1String("Arial Unicode MS") ) )
        {
        unicode_font.setFamily("Arial Unicode MS");
        }
        else if ( ff.contains( QLatin1String("Lucida Unicode MS") ) )
        {
        unicode_font.setFamily("Lucida Unicode MS");
        }
    unicode_font.setPointSize(16);
    unicode_font_str = unicode_font.toString();
    delete fdb;
    }

familyLineEdit->setText( unicode_font.family() );

QObject::connect( toolButton,
                  SIGNAL( pressed() ),
                  this,
                  SLOT( set_font() ) );
QObject::connect( tableView,
                  SIGNAL( pressed(QModelIndex) ),
                  this,
                  SLOT( index_selected(QModelIndex) ) );
QObject::connect( comboBox,
                  SIGNAL( activated(int) ),
                  this,
                  SLOT( reset_model(int) ) ); 
QObject::connect( checkBox,
                  SIGNAL( toggled(bool) ),
                  this,
                  SLOT( update_merging(bool) ) );
QObject::connect( closeButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( close() ) );
QObject::connect( clipboardButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( update_clipboard() ) );                  
                  
}

CharsWidget::~CharsWidget()
{
}

void CharsWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}

void CharsWidget::set_model()
{
QFont font = QFont(unicode_font);

QHeaderView * hh = qobject_cast<QHeaderView *>( tableView->horizontalHeader() );
QHeaderView * vh = qobject_cast<QHeaderView *>( tableView->verticalHeader() );
hh->setFont(default_font);
vh->setFont(default_font);

int height = qMax( 24, QFontMetrics(font).height() + 20 );
int width = qMax( 24, QFontMetrics(font).maxWidth() + 20 );

CharsModel * model = new CharsModel(this);
QList< QVector<int> > ranges;
model->set_data(ranges);

tableView->setHorizontalHeader(hh);
tableView->setVerticalHeader(vh);
tableView->setFont(font);
tableView->setModel(model);

    for ( int i = 0; i < 16; ++i )
    {
    tableView->setColumnWidth( i, width );
    }
    for ( int i = 0; i < model->rowCount(); ++i )
    {
    tableView->setRowHeight( i, height );
    }

    if ( model->rowCount() > 0 )
    {
    tableView->showRow(0);
    }
}

void CharsWidget::set_font()
{
bool ok = false;
QFont font = QFontDialog::getFont(&ok, unicode_font);

unicode_font = QFont(font);
CharsModel * model = qobject_cast<CharsModel *>( tableView->model() );
QHeaderView * hh = qobject_cast<QHeaderView *>( tableView->horizontalHeader() );
QHeaderView * vh = qobject_cast<QHeaderView *>( tableView->verticalHeader() );
int height = qMax( 24, QFontMetrics(unicode_font).height() + 20 );
int width = qMax( 24, QFontMetrics(unicode_font).maxWidth() + 20 );
hh->setFont(default_font);
vh->setFont(default_font);
    for ( int i = 0; i < 16; ++i )
    {
    tableView->setColumnWidth( i, width );
    }
    for ( int i = 0; i < model->rowCount(); ++i )
    {
    tableView->setRowHeight( i, height );
    }
tableView->setHorizontalHeader(hh);
tableView->setVerticalHeader(vh);
tableView->setFont(unicode_font);
tableView->update();
    if ( model->rowCount() > 0 )
    {
    tableView->showRow(0);
    }

checkBox->blockSignals(true);
checkBox->setChecked(true);
checkBox->blockSignals(false);

familyLineEdit->setText( unicode_font.family() );
}

void CharsWidget::index_selected( QModelIndex index )
{
QVariant v = index.data();
QString character_str = v.toString();
QString text = lineEdit->text();
    if ( character_str.length() == 1 )
    {
    QChar character = character_str.data()[0];
    text.append( character );
    lineEdit->setText( text);
    const unsigned short unicode = character.unicode();
    int unicode_int = (int) unicode;
    lcdNumber->display(unicode_int);
    }

}

void CharsWidget::reset_model( int i )
{
QFont font = tableView->font();
int height = qMax( 24, QFontMetrics(font).height() + 20 );
int width = qMax( 24, QFontMetrics(font).maxWidth() + 20 );
CharsModel * old_model = qobject_cast<CharsModel *>( tableView->model() );
CharsModel * model = new CharsModel(this);
QList< QVector<int> > ranges = set_ranges(i);
model->set_data(ranges);

tableView->setModel(model);
    for ( int i = 0; i < 16; ++i )
    {
    tableView->setColumnWidth( i, width );
    }
    for ( int i = 0; i < model->rowCount(); ++i )
    {
    tableView->setRowHeight( i, height );
    }
    
    if ( model->rowCount() > 0 )
    {
    tableView->showRow(0);
    }
    
tableView->update();
delete old_model;
old_model = NULL;
}

void CharsWidget::update_merging( bool enable )
{
QFont font = tableView->font();
    if (enable)
    {
    font.setStyleStrategy(QFont::PreferDefault);
    }
    else
    {
    font.setStyleStrategy(QFont::NoFontMerging);
    }

CharsModel * model = qobject_cast<CharsModel *>( tableView->model() );
QHeaderView * hh = qobject_cast<QHeaderView *>( tableView->horizontalHeader() );
QHeaderView * vh = qobject_cast<QHeaderView *>( tableView->verticalHeader() );
int height = qMax( 24, QFontMetrics(font).height() + 20 );
int width = qMax( 24, QFontMetrics(font).maxWidth() + 20 );
hh->setFont(default_font);
vh->setFont(default_font);
    for ( int i = 0; i < 16; ++i )
    {
    tableView->setColumnWidth( i, width );
    }
    for ( int i = 0; i < model->rowCount(); ++i )
    {
    tableView->setRowHeight( i, height );
    }
tableView->setHorizontalHeader(hh);
tableView->setVerticalHeader(vh);
tableView->setFont(font);
tableView->update();
}

void CharsWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("CharsWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.setValue( QLatin1String("unicode_font"), QVariant( unicode_font.toString() ) );
settings.endGroup();
}

void CharsWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("CharsWidget") );
    if ( true != settings.value( QLatin1String("unicode_font") ).isNull() )
    {
    unicode_font_str = settings.value( QLatin1String("unicode_font") ).toString();
    }
    else
    {
    unicode_font_str = QLatin1String("");
    }
resize( settings.value( QLatin1String("size"), QSize( 720, 550 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void CharsWidget::update_clipboard()
{
clipboard->setText( lineEdit->text(), QClipboard::Clipboard );
clipboard->setText( lineEdit->text(), QClipboard::Selection );
}

QList< QVector<int> > CharsWidget::set_ranges( int i )
{
QList< QVector<int> > ranges;

    if ( i == 0 )
    {
    }
    else if ( i == 2 ) // armenian
    {
    QVector<int> v;
    v.push_back(83);
    v.push_back(88);
    ranges << v;
    QVector<int> v1;
    v1.push_back(4017);
    v1.push_back(4017);
    ranges << v1;
    }
    else if ( i == 3 ) // cyrillic
    {
    QVector<int> v;
    v.push_back(64);
    v.push_back(82);
    ranges << v;
    }
    else if ( i == 4 ) // georgian
    {
    QVector<int> v;
    v.push_back(266);
    v.push_back(271);
    ranges << v;
    QVector<int> v1;
    v1.push_back(720);
    v1.push_back(722);
    ranges << v1;
    }
    else if ( i == 1 ) // arabic
    {
    QVector<int> v;
    v.push_back(96);
    v.push_back(111);
    ranges << v;
    QVector<int> v1;
    v1.push_back(117);
    v1.push_back(119);
    ranges << v1;
    QVector<int> v2;
    v2.push_back(4021);
    v2.push_back(4063);
    ranges << v2;
    QVector<int> v3;
    v3.push_back(4071);
    v3.push_back(4079);
    ranges << v3;
    }
    else if ( i == 6 ) // hebrew
    {
    QVector<int> v;
    v.push_back(89);
    v.push_back(95);
    ranges << v;
    QVector<int> v1;
    v1.push_back(4016);
    v1.push_back(4020);
    ranges << v1;
    }
    else if ( i == 5 ) // greek
    {
    QVector<int> v;
    v.push_back(55);
    v.push_back(63);
    ranges << v;
    QVector<int> v1;
    v1.push_back(496);
    v1.push_back(511);
    ranges << v1;
    }

    else if ( i == 7 ) // currency
    {
    QVector<int> v;
    v.push_back(522);
    v.push_back(523);
    ranges << v;
    QVector<int> v1;
    v1.push_back(10);
    v1.push_back(10);
    ranges << v1;
    QVector<int> v2;
    v2.push_back(2);
    v2.push_back(2);
    ranges << v2;
    }

return ranges;
}
